package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 在DML中使用预编译语句
 */
public class JDBCDemo08 {
    public static void main(String[] args) {
        /*
            使用预编译语句实现注册功能

            INSERT INTO userinfo(username,password,nickname,age)
            VALUES(?,?,?,?);
         */
        UserInfo register = InputUtil.getInputObject(new UserInfo(), "欢迎注册", "register");
        try {
            Connection connection = DBUtil.getConnection();
            String sql = "INSERT INTO userinfo(username,password,nickname,age)\n" +
                         "VALUES(?,?,?,?);";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,register.getUsername());
            ps.setString(2,register.getPassword());
            ps.setString(3,register.getNickname());
            ps.setInt(4,register.getAge());
            int num = ps.executeUpdate();
            if (num>0)
                System.out.println("注册成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
